function max(a, b) {
  return a > b ? a : b
}

function dKnapsack(capacity, size, value, n) {
  var k = []
  for (let i = 0; i <= capacity + 1; i++) {
    k[i] = []
  }
  for (let i = 0; i <= n; i++) {
    for (let w = 0; w <= capacity; w++) {
      if (i === 0 || w === 0) {
        k[i][w] = 0
      } else if (size[i - 1] <= w) {
        k[i][w] = max(value[i - 1] + k[i - 1][w - size[i - 1]], k[i - 1][w])
      } else {
        k[i][w] = k[i - 1][w]
      }
      // putstr(k[i][w] + '')
    }
    console.log(k)
  }
  return k[n][capacity]
}

const value = [4, 5, 10, 11, 13] // 物品价值
const size = [3, 4, 7, 8, 9] // 物品尺寸
const capacity = 16 // 总容量
const n = 5 // 物品个数

console.log(dKnapsack(capacity, size, value, n))
